///////////////////////////////////
// COMMON FUNCTIONS
//////////////////////////////////

// firebug
if (!("console" in window) || !("firebug" in console))
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}
//

function hideAllPages()
{
    toggleVisibility( _gel( "page_news" ), false );
    toggleVisibility( _gel( "page_weather" ), false );
    toggleVisibility( _gel( "page_topList" ), false );
    toggleVisibility( _gel( "page_search" ), false );
    toggleVisibility( _gel( "page_profile" ), false );
    //     toggleVisibility( _gel( "requestError" ), false );
}

function makeClassHandler( obj )
{

    if ( obj.className.length > 0 ) {
	obj.classes = obj.className.split( ' ' );
    }
    else {
	obj.classes = new Array();
    }

    obj.addClass = function( c ) {
	for ( var i = 0 ; i < this.classes.length; i++ ) {
	    if ( this.classes[i] == c ) {
		return true;
	    }
	}
	this.classes.push( c );
	this.updateClassName();
	return true;
    }

    obj.dropClass = function( c ) {
	for ( var i = 0 ; i < this.classes.length; i++ ) {
	    if ( this.classes[i] == c ) {
		//		this.classes[i] = null;
		this.classes.splice( i, 1 );
		this.updateClassName();
		return true;
	    }
	}
	return false;
    }

    obj.hasClass = function( c ) {
	for ( var i = 0 ; i < this.classes.length; i++ ) {
	    if ( this.classes[i] == c ) {
		return true;
	    }
	}
	return false;
    }
    
    obj.updateClassName = function(){
	this.className = this.classes.join( ' ' );
    }

}

function toggleVisibility( param_element, param_visible )
{
    if ( param_element.style ) {
	param_element.style.display = ( param_visible ? "block" : "none" );

	if ( param_visible ) {
	    // show body as well
	    var body = _gelstn( "body" )[0];
	    body.style.display = "block";
	}
    }
}




///////////////////////////////////
// string.parseJSON(filter)



(function (s) {

     // Augment String.prototype. We do this in an immediate anonymous function to
     // avoid defining global variables.

     // m is a table of character substitutions.

     var m = {
         '\b': '\\b',
         '\t': '\\t',
         '\n': '\\n',
         '\f': '\\f',
         '\r': '\\r',
         '"' : '\\"',
         '\\': '\\\\'
     };


     s.parseJSON = function (filter) {
         var j;

         function walk(k, v) {
             var i, n;
             if (v && typeof v === 'object') {
                 for (i in v) {
                     if (Object.prototype.hasOwnProperty.apply(v, [i])) {
                         n = walk(i, v[i]);
                         if (n !== undefined) {
                             v[i] = n;
                         }
                     }
                 }
             }
             return filter(k, v);
         }


	 // Parsing happens in three stages. In the first stage, we run the text against
	 // a regular expression which looks for non-JSON characters. We are especially
	 // concerned with '()' and 'new' because they can cause invocation, and '='
	 // because it can cause mutation. But just to be safe, we will reject all
	 // unexpected characters.

	 // We split the first stage into 4 regexp operations in order to work around
	 // crippling deficiencies in IE's and Safari's regexp engines. First we replace
	 // all backslash pairs with '@' (a non-JSON character). Second, we replace all
	 // simple value tokens with ']' characters. Third, we delete all open brackets
	 // that follow a colon or comma or that begin the text. Finally, we look to see
	 // that the remaining characters are only whitespace or ']' or ',' or ':' or '{'
	 // or '}'. If that is so, then the text is safe for eval.

         if (/^[\],:{}\s]*$/.test(this.replace(/\\./g, '@').
				  replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(:?[eE][+\-]?\d+)?/g, ']').
                    replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

// In the second stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.

                j = eval('(' + this + ')');

// In the optional third stage, we recursively walk the new structure, passing
// each name/value pair to a filter function for possible transformation.

                return typeof filter === 'function' ? walk('', j) : j;
            }

// If the text is not JSON parseable, then a SyntaxError is thrown.

            throw new SyntaxError('parseJSON');
        };


        s.toJSONString = function () {

// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can simply slap some quotes around it.
// Otherwise we must also replace the offending characters with safe
// sequences.

            if (/["\\\x00-\x1f]/.test(this)) {
                      return '"' + this.replace(/[\x00-\x1f\\"]/g, function (a) {
                    var c = m[a];
                    if (c) {
                        return c;
                    }
                    c = a.charCodeAt();
                    return '\\u00' + Math.floor(c / 16).toString(16) +
                                               (c % 16).toString(16);
                }) + '"';
            }

            return '"' + this + '"';
        };
    })(String.prototype);